//
//  WKMonetaryUnitViewController.m
//  ourToken
//
//  Created by wakeup on 2018/6/2.
//  Copyright © 2018年 ourtoken. All rights reserved.
//

#import "WKMonetaryUnitViewController.h"
#import "WKBaseHeader.h"

@interface WKMonetaryUnitViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *footerView;//tableView底部view
@property (nonatomic, strong) UIButton *saveBtn;//保存按钮
@property (nonatomic, copy) NSString *currencyType;//当前选择的货币类型
@property (nonatomic, strong) NSArray *dataArray;//数据源

@end

@implementation WKMonetaryUnitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationView];
    [self layoutUI];
    self.view.backgroundColor = [UIColor whiteColor];
    self.dataArray = @[@"CNY",@"USD"];
    self.currencyType = [[NSUserDefaults standardUserDefaults] valueForKey: @"currencyType"];
}

- (void)setNavigationView {
    [super setNavigationView];
    self.navigationView.topTitleLabel.text = @"货币单位";
}

- (void)layoutUI {
    [self.view addSubview:self.tableView];
    [self.footerView addSubview:self.saveBtn];
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MonetaryUnitCell"];
    cell.textLabel.text = self.dataArray[indexPath.row];
    cell.textLabel.font = AppManager.tableTextFont;
    
    if ([self.currencyType isEqualToString:@"CNY"] && indexPath.row == 0) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 15)];
        imageView.image = [UIImage imageNamed:@"gouqing"];
        cell.accessoryView = imageView;
    }
    
    if ([self.currencyType isEqualToString:@"USD"] && indexPath.row == 1) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 15)];
        imageView.image = [UIImage imageNamed:@"gouqing"];
        cell.accessoryView = imageView;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        self.currencyType = @"CNY";
    }
    else if (indexPath.row == 1) {
        self.currencyType = @"USD";
    }
    [[NSUserDefaults standardUserDefaults] setObject:self.currencyType forKey:@"currencyType"];
    [self.tableView reloadData ];
}

//点击保存
- (void)clickSaveBtn {
    [httpManager setCurrencyType:self.currencyType success:^(NSDictionary *result) {
        
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - 懒加载
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, AppManager.navigationHeight, AppManager.width, AppManager.height-AppManager.navigationHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = AppManager.tableBackColor;
        _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, AppManager.width, 10)];
        _tableView.tableFooterView = self.footerView;
    }
    return _tableView;
}

- (UIView *)footerView {
    if (_footerView == nil) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, AppManager.width, 200)];
        _footerView.backgroundColor = AppManager.tableBackColor;
    }
    return _footerView;
}

- (UIButton *)saveBtn {
    if (_saveBtn == nil) {
        _saveBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        _saveBtn.frame = CGRectMake(50, 30, AppManager.width-50*2, 44);
        [_saveBtn setTitle:@"保存" forState:UIControlStateNormal];
        [_saveBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _saveBtn.backgroundColor = AppManager.themeColor;
        _saveBtn.titleLabel.font = AppManager.btnFont;
        _saveBtn.layer.cornerRadius = 5;
        _saveBtn.layer.masksToBounds = YES;
        [_saveBtn addTarget:self action:@selector(clickSaveBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}


@end
